// pages/case/list/index.js
const app = getApp()
import api from '../../../api/main.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    page: 1,
    pageSize: 3,
    caseList: [],
    isRefresh: true,
    isMoreHid: false,
    isData: true,
    total: 0,
    noDataMessage: '暂时还没有任何执行中的案件哦~'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //加载数据
    that.loadData()
  },
  /**
   * 加载数据
   */
  loadData() {
    const that = this
    if (that.data.page == 1) {
      that.setData({
        isRefresh: true,
        caseList: []
      })
    } else {
      that.setData({
        isMoreHid: true
      })
    }
    var keywords = ''
    var stageIds = ''
    var currentFlag = ''
    var peopleLivehoodIds = ''
    var params = {
      page: that.data.page,
      pageSize: that.data.pageSize,
      status: that.data.currentTab == 0 ? '3' : '4',
      keywords: keywords,
      stageIds: stageIds,
      currentFlag: currentFlag,
      peopleLivehoodIds: peopleLivehoodIds
    }
    api.cases.getCaseList(params).then(res => {
      var isData = false
      const noDataMessage = that.data.currentTab == 0 ? '暂时还没有任何执行中的案件哦~' : '暂时还没有任何已结案的案件哦~'
      var array = []
      const total = res.total
      if (res.code == app.globalData.successCode && res.success) {
        array = res.data
        if (array.length > 0) {
          for (var i = 0; i < array.length; i++) {
            var item = array[i]
            //加入民生类型名称
            if (item.peopleLivehoodId && item.peopleLivehoodId>0){
              for (var j = 0; j < app.globalData.peopleLivehoodList.length;j++){
                const peopleLivehood = app.globalData.peopleLivehoodList[j]
                if (peopleLivehood.id == item.peopleLivehoodId){
                  item["peopleLivehoodName"] = peopleLivehood.name
                }
              }
            }else{
              item["peopleLivehoodName"] = ''
            }
            //判断操作权限
            if (item.status == 3) {
              if (app.globalData.userType == 1) {
                item['modify'] = true
                if (item.employeeId && app.globalData.userData.id == item.employeeId) {
                  item['rewardName'] = !item.isReward || item.isReward == 0 ? "申请悬赏" : "查看悬赏"
                  item['appleReward'] = !item.isReward || item.isReward == 0 ? true : false
                }
              } else {
                item['modify'] = false
                item['rewardName'] = !item.isReward || item.isReward == 0 ? "申请悬赏" : "查看悬赏"
                item['appleReward'] = !item.isReward || item.isReward == 0 ? true : false
              }

            }
            that.data.caseList.push(item)
            //appleReward rewardName modify
            item = null
          }
          isData = true
        }
      }
      if (that.data.page == 1) {
        that.setData({
          isRefresh: false,
          isData: isData,
          caseList: that.data.caseList,
          total: total,
          noDataMessage: noDataMessage
        })
      } else {
        that.setData({
          isMoreHid: false,
          isData: isData,
          caseList: that.data.caseList,
          total: total,
          noDataMessage: noDataMessage
        })
        array = null
      }
      res = null
    })
  },
  /**
   * 点击tab切换
   */
  swichNav: function(e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        page: 1,
        isData: true
      })
      that.loadData()
    }
  },
  /**案件悬赏事件
   * @param item 数据实体
   */
  onCaseReward(e) {
    const item = e.currentTarget.dataset.item
    if (item.appleReward){
      console.log('申请悬赏')
    }else{
      console.log('查看悬赏')
    }
    console.log(item)
  },
  /**修改案件事件 
   * @param item 数据实体
   */
  onModify(e) {
    const item = e.currentTarget.dataset.item
    console.log(item)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log('下拉刷新');
    wx.stopPullDownRefresh()
    this.setData({
      page: 1
    })
    this.loadData()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    console.log('上拉加载更多');
    if (that.data.caseList && that.data.total > that.data.pageSize * that.data.page) {
      that.setData({
        page: that.data.page + 1
      })
      that.loadData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})